package com.andbase.demo.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.andbase.demo.R;
import com.andbase.library.utils.AbStrUtil;
import com.andbase.library.utils.AbToastUtil;
import com.andbase.library.view.calendar.AbCalendarCell;
import com.andbase.library.view.calendar.AbCalendarView;
import com.andbase.library.view.listener.AbOnItemClickListener;
import com.andbase.library.view.recycler.AbRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class CalendarListAdapter extends AbRecyclerViewAdapter {

    private Activity activity;
    List<String> menseslist = null;
    List<String> items = null;

    public CalendarListAdapter(Context context, List<String> items) {
        super(context,items);
        this.activity = (Activity)context;
        this.items = items;
    }

    @Override
    public RecyclerView.ViewHolder onCreateRecyclerViewHolder(ViewGroup parent, int viewType) {
        //必须这么写，不能用View.inflate
        View view = LayoutInflater.from(activity).inflate(R.layout.item_calendar_list, parent, false);
        return new RecyclerViewHolder(view);
    }

    @Override
    public void onBindRecyclerViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final RecyclerViewHolder viewHolder = (RecyclerViewHolder)holder;

        String  itemDate = items.get(position);
        String [] yearmonth = itemDate.split("-");
        Calendar currentCalendar = Calendar.getInstance();
        currentCalendar.set(Calendar.YEAR, Integer.parseInt(yearmonth[0]));
        currentCalendar.set(Calendar.MONTH, Integer.parseInt(yearmonth[1])-1);
        currentCalendar.set(Calendar.DAY_OF_MONTH, 1);

        viewHolder.calendarTitle.setText(yearmonth[0] + "-" + AbStrUtil.strFormat2(String.valueOf(Integer.parseInt(yearmonth[1]))));

        viewHolder.calendarView.setCalendar(currentCalendar);

        List<String> selectedDateList = new ArrayList<>();
        selectedDateList.add("2019-02-10");
        selectedDateList.add("2019-02-11");
        selectedDateList.add("2019-02-12");

        List<String> remindDateList = new ArrayList<>();
        remindDateList.add("2019-03-04");
        remindDateList.add("2019-03-05");
        remindDateList.add("2019-03-06");

        List<String> recordDateList = new ArrayList<>();
        recordDateList.add("2019-02-20");
        recordDateList.add("2019-02-21");
        recordDateList.add("2019-02-22");

        List<String> attendDateList = new ArrayList<>();
        attendDateList.add("2019-03-20");
        attendDateList.add("2019-03-21");
        attendDateList.add("2019-03-22");

        List<String> importantDateList = new ArrayList<>();
        importantDateList.add("2019-03-23");

        viewHolder.calendarView.setSelectedDateList(selectedDateList);

        viewHolder.calendarView.setAttendDateList(attendDateList);

        viewHolder.calendarView.setRecordDateList(recordDateList);

        viewHolder.calendarView.setRemindDateList(remindDateList);

        viewHolder.calendarView.setImportantDateList(importantDateList);

        List<AbCalendarCell> calendarCells  = viewHolder.calendarView.getCalendarCells();
        for (int i = 0; i < calendarCells.size(); i++) {
            AbCalendarCell calendarCellOther = calendarCells.get(i);
            if(calendarCellOther.getCellDate().getTimeInMillis() <= System.currentTimeMillis()){
                calendarCellOther.setEditable(true);
            }else{
                calendarCellOther.setEditable(false);
            }
        }


        viewHolder.calendarView.setOnItemClickListener(new AbOnItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {

                //往提交列表里面加  有就删除  没有就新增  最后能得到要提交的数据
                AbToastUtil.showToast(activity,viewHolder.calendarView.getDate(i));
            }
        });

    }


    public class RecyclerViewHolder extends RecyclerView.ViewHolder {

        View itemView;
        TextView calendarTitle;
        AbCalendarView calendarView;

        public RecyclerViewHolder(View view) {
            super(view);
            itemView = view;
            calendarTitle = (TextView)itemView.findViewById(R.id.calendar_title);
            calendarView = (AbCalendarView)itemView.findViewById(R.id.calendar_view);
        }
    }

}
